package ye.guo.huang.datatransferdemo.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.logging.Logger;


public class AESUtils {
	private static final Logger log = Logger.getLogger(AESUtils.class.getName());

	public static String Encrypt(String sSrc, String sKey) throws Exception {
		byte[] raw = sKey.getBytes("utf-8");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(1, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
		return Base64.getEncoder().encodeToString(encrypted);
//		return (new Base64()).encodeToString(encrypted);
	}

	public static String Decrypt(String sSrc, String sKey) {
		try {
			if (sKey == null) {
				log.info("key isNull");
				return null;
			} else {
				byte[] raw = sKey.getBytes("utf-8");
				SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
				cipher.init(2, skeySpec);
//				byte[] encrypted1 = (new Base64()).decode(sSrc);
				byte[] encrypted1 =  Base64.getDecoder().decode(sSrc);

				try {
					byte[] original = cipher.doFinal(encrypted1);
					String originalString = new String(original, "utf-8");
					return originalString;
				} catch (Exception var8) {
					log.info(var8.toString());
					return null;
				}
			}
		} catch (Exception var9) {
			log.info(var9.toString());
			return null;
		}
	}

	public static void main(String[] args) throws Exception {
		//确认预约数据已同步
		String cKey = "1ced745453a3fb71";
//		String str = "{\"successOrderIds\":[\"021ebf240e7f4f649600a49973f2375d\",\"0de32fd6b9a34674b6e2019d8c02388e\",\"09321853842049e8a2104bc69f5a3403\"]}";
//		System.out.println(str);
//		String enString = Encrypt(str, cKey);
//		System.out.println("加密后的字串是：" + enString);
//		enString = "k7cJzoymVjIdyp9oyTK6rJuRqYJjYkQuLHbzysqZqF3Wu1tck1rGbHVCoZFBctDW2wqaHAHWHqW7/orBAowX/qs9DoKP9l1IZZfEijd83zoXjMn+0QZMbhLRqOi/WJbKS6St6lmydq4lCcBw5R3ttCnk2GoObWiLGH5Zn8SjaVk=";
//		String DeString = Decrypt(enString, cKey);
//		System.out.println("解密后的字串是：" + DeString);

//		String str = "{\"userId\":\"1559545341674295297\"}";
//		System.out.println(str);
//		String enString = Encrypt(str, cKey);
//		System.out.println("加密后的字串是：" + enString);
//		enString = "jywIF3YadWWGgIXjhsPQZti9/ENUpzEVP/gA4q2PkF5Hk6F9+W3vH2GL14cv7R5W";
//		String DeString = Decrypt(enString, cKey);
//		System.out.println("解密后的字串是：" + DeString);


//		String str = "{\"passRecords\":[{\"userId\":\"1559545341674295297\",\"siteId\":\"1559545341674295297\",\"inOutType\":1,\"passType\":1,\"time\":\"2022-10-22 11:11:11\"}]}";
//		System.out.println(str);
//		String enString = Encrypt(str, cKey);
//		System.out.println("加密后的字串是：" + enString);
//		enString = "Bg4Io1c3yTfmUKiJt1WFhY8sCBd2GnVlhoCF44bD0Gb+Lyg4I9X235ygsErD9btZpatlVNlgkS1Jo1wa9a8nTGxU7aoeRrYdmPokLnaFoMXrKPsk1SjGZ5YGEAAOCqy54fEDs4hZ3sSkertMUTGVzZoN4wxDEvHLifmFDwnQ7tta522PiZuP5L/4fE9/tN0i";
//		String DeString = Decrypt(enString, cKey);
//		System.out.println("解密后的字串是：" + DeString);

		cKey = "949c41ada5552ce3";
		String str = "{\"startUpTime\":\"2022-10-22 12:12:12\",\"currentTime\":\"2022-10-22 12:12:12\"}";
		System.out.println(str);
		String enString = Encrypt(str, cKey);
		System.out.println("加密后的字串是：" + enString);
		enString = "Oik1AAjN43nCoitAKC88cSEw6T1/Vjj1VOm3gH3qHxiAoKhbXmP/JNStqsJpnNpC0kVTd9ofIfMLKKv08/308aG8jgZiMO+czv10egTvMVk=";
		String DeString = Decrypt(enString, cKey);
		System.out.println("解密后的字串是：" + DeString);


//		String code = "\"" + "1559545341674295297," + System.currentTimeMillis() +"\"";
//		String str = "{\"qrcode\":"+code+"}";
//		System.out.println(str);
//		String enString = Encrypt(str, cKey);
//		System.out.println("加密后的字串是：" + enString);
//		enString = "3dMNwUSjoH+iUGcdLdkO4hYz1c8h1ppvCSFvqnwY68VpTuHHxWWPKquBFN3j+Sb9";
//		String DeString = Decrypt(enString, cKey);
//		System.out.println("解密后的字串是：" + DeString);
	}
}
